package com.lh.arithmetic.interview.easyprogram;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 ************************************************
 ** Module : 设计4个线程，其中两个线程每次对j增加1，
 ** 另外两个线程对j每次减少1。改进版 用原子类型
 ** Date: 2013-3-15  Time: 下午04:48:38 
 ** Author lh 
 ************************************************ 
 */
public class TwoThreadIncTwoThreadDecOptime1 {
	public static void main(String[] args) {
		final AtomicInteger i = new AtomicInteger();
		
		Runnable increment = new Runnable() {
			
			public void run() {
				int temp = i.incrementAndGet();
				System.out.println("增加了，值为：" + temp);
			}
		};
			
		Runnable decrement = new Runnable() {
			
			public void run() {
				int temp = i.decrementAndGet();
				System.out.println("减少了，值为：" + temp);
			}
		};
		ExecutorService exec = Executors.newFixedThreadPool(4);
		
		exec.submit(increment);
		exec.submit(increment);
		exec.submit(decrement);
		exec.submit(decrement);
		
		exec.shutdown();
	}
}
